package com.controller.admin;

import com.bean.bo.GoodTypeAdd;
import com.bean.bo.good.*;
import com.bean.bo.message.ReplyBO;
import com.bean.pojo.GoodsType;
import com.bean.pojo.Space;
import com.bean.vo.admin.Result;
import com.bean.vo.goods.GoodDetailsVO;
import com.bean.vo.goods.SpecVO;
import com.bean.vo.goods.TypeGoodVO;
import com.bean.vo.message.ShowNoReplyMessageVO;
import com.bean.vo.message.ShowReplyMessageVO;
import com.google.gson.Gson;
import com.service.admin.GoodsServiceImplement;
import com.utils.GsonUtils;
import com.utils.upload;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {
    private static final Gson gson= GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("getType".equals(action)){
            showGoodsType(request,response);
        }

        if ("getGoodsByType".equals(action)){
            showTypeGoods(request,response);
        }
        if ("deleteGoods".equals(action)){
            deleteGood(request,response);
        }
        if ("deleteType".equals(action)){
            deleteGoodType(request,response);
        }
        if ("getGoodsInfo".equals(action)){
            showGoodDetails(request,response);
        }
        if ("noReplyMsg".equals(action)){
            showMessage(request,response);
        }
        if ("repliedMsg".equals(action)){
            showReplyMessage(request,response);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/admin/goods/", "");
        if ("addType".equals(action)){
            addType(request,response);
        }
        if ("addGoods".equals(action)){
            addGood(request,response);
        }
        if ("imgUpload".equals(action)){
            try {
                upload(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if ("addSpec".equals(action)){
            addSpaceByUpdateGood(request,response);
        }
        if ("deleteSpec".equals(action)){
            deleteSpecByUpdate(request,response);
        }
        if ("updateGoods".equals(action)){
            updateGood(request,response);
        }
        if ("reply".equals(action)){
            reply(request,response);
        }

    }

    /**
     * 回复留言
     * @param request
     * @param response
     */
    private void reply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
        ReplyBO replyBO = gson.fromJson(requestString, ReplyBO.class);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int replyState = goodsServiceImplement.replyMessage(replyBO);
        if (replyState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"回复失败")));
    }


    /**
     * 显示已回复留言
     * @param request
     * @param response
     */
    private void showReplyMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        List<ShowReplyMessageVO> showReplyMessageVOS = goodsServiceImplement.showReplyMessage();
        response.getWriter().println(gson.toJson(new Result(0,showReplyMessageVOS,null)));
    }

    /**
     * 显示未回复留言
     * @param request
     * @param response
     */
    private void showMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        List<ShowNoReplyMessageVO> showNoReplyMessageVOS = goodsServiceImplement.showMessage();
        response.getWriter().println(gson.toJson(new Result(0,showNoReplyMessageVOS,null)));

    }


    /**
     * 修改商品数据
     * @param request
     * @param response
     */
    private void updateGood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        UpdateGoodBO updateGoodBO = gson.fromJson(requestString, UpdateGoodBO.class);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int updateState = goodsServiceImplement.updateGood(updateGoodBO);
        if (updateState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"修改失败")));
    }

    /**
     * 修改时删除space
     * @param request
     * @param response
     */
    private void deleteSpecByUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
        SpaceBOByUpdate spaceBOByUpdate = gson.fromJson(requestString, SpaceBOByUpdate.class);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int deleteState = goodsServiceImplement.deleteSpaceByUpdate(spaceBOByUpdate);
        if (deleteState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"删除失败")));

    }

    /**
     * 商品修改页面中的添加Space
     * @param request
     * @param response
     */
    private void addSpaceByUpdateGood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
        AddSpaceBO space = gson.fromJson(requestString, AddSpaceBO.class);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int addState = goodsServiceImplement.addGoodSpaceByUpdate(space);
        if (addState==200){
            response.getWriter().println(gson.toJson(new Result(0,space,null)));
            return;
        }

        response.getWriter().println(gson.toJson(new Result(10000,null,"增加失败")));
    }

    /**
     * 修改商品页面商品详情回显
     * @param request
     * @param response
     */
    private void showGoodDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id = Integer.parseInt(idString);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        GoodDetailsVO goodDetailsVO = goodsServiceImplement.showGoodDetails(id);
        response.getWriter().println(gson.toJson(new Result(0,goodDetailsVO,null)));
    }

    /**
     * 删除商品分类
     * @param request
     * @param response
     * @Author xiao-fan
     */
    private void deleteGoodType(HttpServletRequest request, HttpServletResponse response) {

    }

    /**
 * @Author xiao-fan
 * @Description  删除商品
 * @Date 23:16 2021/6/17
     * @param request
     * @param response
 **/
    private void deleteGood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idString = request.getParameter("id");
        int id=Integer.parseInt(idString);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int deleteState = goodsServiceImplement.deleteGood(id);
        if (deleteState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"删除失败")));

    }
/**
 * @Author xiao-fan
 * @Description  上传图片
 * @Date 22:45 2021/6/17
 **/
    private void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String map = upload.getMap(request);
        map="http://localhost:8084/"+map;
        response.getWriter().println(gson.toJson(new Result(0,map,null)));
    }

    /**
 * @Author xiao-fan
 * @Description  添加商品
 * @Date 20:04 2021/6/17
 **/
    private void addGood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        AddGoodBO addGoodBO = gson.fromJson(requestString, AddGoodBO.class);
        List<SpaceBO> specList = addGoodBO.getSpecList();
        for (SpaceBO spaceBO : specList) {
            try {
                Double.parseDouble(spaceBO.getUnitPrice());
                Integer.parseInt(spaceBO.getStockNum());
            } catch (NumberFormatException e) {
                response.getWriter().println(gson.toJson(new Result(10000,null,"请输入正确的参数")));
                return;
            }

        }
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int addState = goodsServiceImplement.addGood(addGoodBO);
        if (addState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
        }
        else {
            response.getWriter().println(gson.toJson(new Result(10000,null,"添加失败")));
        }


    }

    /**
 * @Author xiao-fan
 * @Description  显示类目下得商品简介
 * @Date 17:48 2021/6/17
 **/
    private void showTypeGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeIdString = request.getParameter("typeId");
        int typeId = Integer.parseInt(typeIdString);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        List<TypeGoodVO> typeGoodVOSList = goodsServiceImplement.selectToJson(typeId);
        response.getWriter().println(gson.toJson(new Result(0,typeGoodVOSList,null)));

    }

    /**
 * @Author xiao-fan
 * @Description  添加分类操作
 * @Date 14:25 2021/6/17
 **/
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        GoodTypeAdd goodTypeAdd = gson.fromJson(requestString, GoodTypeAdd.class);
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        int addState = goodsServiceImplement.addGoodsType(goodTypeAdd);
        if (addState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        else {
            response.getWriter().println(gson.toJson(new Result(10000,null,"该种类已存在")));
        }

    }


/**
 * @Author xiao-fan
 * @Description  显示商品分类标签
 * @Date 11:40 2021/6/17
 **/
    private void showGoodsType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GoodsServiceImplement goodsServiceImplement = new GoodsServiceImplement();
        List<GoodsType> goodsTypesList = goodsServiceImplement.showAllGoodsTypes();
        response.getWriter().println(gson.toJson(new Result(0,goodsTypesList,null)));
    }
}
